import Api_works from '../../common/api/works.js'

const state = {
	//类型列表
	get categoryList(){
		return uni.getStorageSync('categoryList') || []
	},
	set categoryList(value){
		uni.setStorageSync('categoryList',value)
	},
	//获取草稿箱
	draftList:[],
	castList:[],//铸造列表
	isRefresh:false,//是否刷新藏品列表
	isRefreshMarket:false,//是否刷新市场列表
	isRefreshOrder:false,//是否刷新订单列表
	titleCurrent:0,//藏品切换下标
	marketCurrent:0//市场切换下标
}
const mutations = {
	SET_CATEGORY(state,payload){
		state.categoryList = payload
	},
	SET_DRAFTLIST(state,payload){
		state.draftList = payload
	},
	SET_CASTLIST(state,payload){
		state.castList = payload
	},
	SET_ISREFRESH(state,payload){
		state.isRefresh = payload
	},
	SET_ISREFRESHMARKET(state,payload){
		state.isRefreshMarket = payload
	},
	SET_TITLECURRENT(state,payload){
		state.titleCurrent = payload
	},
	SET_MARKETCURRENT(state,payload){
		state.marketCurrent = payload
	},
	SET_ISREFRESHORDER(state,payload){
		state.isRefreshOrder = payload
	}
}
const actions = {
	//用户主页专辑列表
	albumAll({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_works.albumAll(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//用户主页作品列表
	albumWorks({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_works.albumWorks(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//草稿箱及已铸造列表
	work({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_works.work(payload).then(res => {
				const data = res.data.data
				if(payload.status == 0){
					if(payload.page > 1){
						commit('SET_DRAFTLIST',[...state.draftList,...data])
					}else{
						commit('SET_DRAFTLIST',data)
					}
				}else{
					if(payload.page > 1){
						commit('SET_CASTLIST',[...state.castList,...data])
					}else{
						commit('SET_CASTLIST',data)
					}
				}
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//类型列表
	category({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_works.category(payload).then(res => {
				const {data} = res
				commit('SET_CATEGORY',data)
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//保存作品
	workStore({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_works.workStore(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//铸造
	workMint({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_works.workMint(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//删除作品
	workDelete({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_works.workDelete(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//市场
	market({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_works.market(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//市场详情
	marketShow({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_works.marketShow(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//挂卖列表
	marketSells({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_works.marketSells(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//作品搜索
	searchWorks({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_works.searchWorks(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//用户搜索
	searchUser({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_works.searchUser(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//盲盒市场
	boxMarket({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_works.boxMarket(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//盲盒市场详情
	boxMarketShow({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_works.boxMarketShow(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//盲盒寄售列表
	boxMarketSell({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_works.boxMarketSell(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
	//优先购
	preList({ commit }, payload) {
		return new Promise((resolve, reject) => {
			Api_works.preList(payload).then(res => {
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	},
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
